package com.mazegame.frontend;


import com.mazegame.backend.BackgroundPanel;
import com.mazegame.backend.ImageDisplayPane;
import com.mazegame.backend.UserDAO;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.shape.Rectangle;

import javax.swing.*;

import javafx.scene.layout.BorderPane;
import java.awt.*;
import javafx.scene.control.Label;
import javafx.geometry.Insets;

public class MainPanel extends JPanel implements GamePanel{
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;

    private static HBox content;
    private static VBox imformationBox;
    private static Label nameLabel;
    private static Label idLabel;
    private static Label coin;

    public MainPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;//
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);
        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/主界面副本.png", 1
        );
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);
        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层
        Platform.runLater(() -> {
            // 创建主容器
            BorderPane root = createMainPane();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

    private BorderPane createMainPane() {
        BorderPane root = new BorderPane();
        root.setTop(createTitleRegion());
        root.setCenter(createContentRegion());
        return root;
    }

    private Region createTitleRegion() {
        HBox titleBox = new HBox();
        titleBox.setPadding(new Insets(20, 0, 0, 30));
        // 设置宽高
        titleBox.setPrefSize(WIDTH, 150);
        // 设置背景颜色,辅助布局
        //titleBox.setStyle("-fx-background-color: rgb(255,255,255);");
        Button button = new Button();
        ImageDisplayPane imageDisplayPane = new ImageDisplayPane("/img/返回按钮.png", 80, 80, 0);
        button.setGraphic(imageDisplayPane);
        button.setStyle("-fx-background-color:transparent; -fx-background-radius: 200;");
        button.setEffect(null);
        button.setPrefSize(100, 100);
        // 设置最大宽高度
        button.setMaxSize(100, 100);
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: rgba(255,255,255,0.5); -fx-background-radius: 200;");
            //button.setEffect(new javafx.scene.effect.DropShadow(10, Color.rgb(200, 200, 200, 1)));
        });
        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: transparent; -fx-background-radius: 200;");
            button.setEffect(null);
        });
        // 添加点击效果
        button.setOnAction(e -> {
            cardLayout.show(cardPanel, "LOGINPANEL");
        });
        titleBox.getChildren().addAll(button);

        return titleBox;
    }

    private Region createContentRegion() {
        content = new HBox();
        content.setPadding(new Insets(0));
        // 设置宽
        content.setPrefSize(WIDTH, 650);
        content.setMaxSize(WIDTH, 650);
        Region user = userRegion();
        Region button = buttonRegion();
        // 设置背景颜色,辅助布局
        //content.setStyle("-fx-background-color: rgb(101,64,64);");
        // 设置背景颜色为透明
        content.setStyle("-fx-background-color: transparent;");  // 关键点：设置为透明
        // 使用 Spacer 填充中间的空白区域
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        // 设置边距
        HBox.setMargin(user, new Insets(430, 0, 0, 65)); // 左侧边距为 0
        HBox.setMargin(button, new Insets(220, 52, 0, 0)); // 左侧边距为 0，顶部边距为 50
        content.getChildren().addAll(user, spacer, button);
        return content;
    }

    private static Region userRegion() {
        imformationBox = new VBox(30);
        imformationBox.setPrefSize(300, 150);
        imformationBox.setMaxSize(300, 150);
        javafx.scene.shape.Rectangle clip = new Rectangle(300, 150);
        clip.setArcWidth(50);
        clip.setArcHeight(50);
        imformationBox.setClip(clip);
        imformationBox.setSpacing(20);
//        imformationBox.setStyle(
//                "-fx-background-color: transparent;" +
//                        "-fx-border-radius: 27;" +
//                        "-fx-border-color: #ffffff; -fx-border-width: 4;"
//        );
        imformationBox.setStyle(
                "-fx-background-color: linear-gradient(to bottom right, #add3ef, #edf4a0);" +
                        "-fx-border-radius: 25;" +
                        "-fx-border-color: #ffffff; -fx-border-width: 4;" +
                        "-fx-padding: 20;"
        );

        imformationBox.setEffect(new javafx.scene.effect.DropShadow(25, Color.rgb(1, 1, 1, 0.5)));

        nameLabel = new Label("名称：加载中...");
        idLabel = new Label("ID：加载中...");
//        nameLabel.setStyle("-fx-text-fill: white; -fx-font-size: 30;");
//        idLabel.setStyle("-fx-text-fill: white; -fx-font-size: 30;");
        nameLabel.setStyle(
                "-fx-text-fill: white;" +
                        "-fx-font-size: 32;" +
                        "-fx-font-family: 'Segoe UI';" +
                        "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 4, 0, 0, 0);"
        );

        idLabel.setStyle(
                "-fx-text-fill: white;" +
                        "-fx-font-size: 30;" +
                        "-fx-font-family: 'Segoe UI';" +
                        "-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 4, 0, 0, 0);"
        );


        imformationBox.setPadding(new Insets(20, 20, 20, 20));
        imformationBox.getChildren().addAll(nameLabel, idLabel);
//
        imformationBox.setEffect(new DropShadow(20, Color.rgb(0, 0, 0, 1)));

        return imformationBox;
    }

    public static void updateUserRegion(){
        imformationBox.getChildren().removeAll(nameLabel, idLabel);
        // 获取当前登录的用户名和ID
        String userName = LoginPanel.currentUsername;
        String userId = LoginPanel.currentUserId;
        nameLabel = new Label("名称：" + (userName != null ? userName : "加载中..."));
        idLabel = new Label("ID：" + (userId != null ? userId : "加载中..."));
        nameLabel.setStyle("-fx-text-fill: white; -fx-font-size: 30;");
        idLabel.setStyle("-fx-text-fill: white; -fx-font-size: 30;");
        imformationBox.getChildren().addAll(nameLabel, idLabel);
        //coin.setText("金币：" + UserDAO.getScore(Integer.parseInt(LoginPanel.currentUserId)));
    }
    private Region buttonRegion() {
        Region button1 = createButton("模式选择");
        button1.setOnMouseClicked(e -> {
            cardLayout.show(cardPanel, "OPTIONPANEL");
        });
        Region region = new Region();
        region.setPrefSize(0,5);
        //region.setStyle("-fx-background-color: rgb(171,163,236,0.8);");
        Region button2 = createButton("角色信息");
        button2.setOnMouseClicked(e -> {
            cardLayout.show(cardPanel, "CHARACTERPANEL");
        });
        Region button3 = createButton("排行榜");
        button3.setOnMouseClicked(e -> {
            RankPanel.update2();
            cardLayout.show(cardPanel, "RANKPANEL");
        });
        VBox buttonBox = new VBox(50);
        buttonBox.setPrefSize(310, 480);
        buttonBox.setMaxSize(310, 480);
        buttonBox.setStyle("-fx-background-color: transparent;");
        buttonBox.getChildren().addAll(button1,button2,button3);
        return buttonBox;
    }

    private Button createButton(String text) {
        Button button = new Button();
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            //button.setStyle("-fx-background-color: rgb(25,160,248,0.3); -fx-background-radius: 65;");
            button.setStyle("-fx-background-color: transparent; -fx-background-radius: 65;");

            button.setEffect(new javafx.scene.effect.DropShadow(10, javafx.scene.paint.Color.rgb(108, 92, 231, 0.5)));
        });

        button.setOnMouseExited(e -> {
            //button.setStyle("-fx-background-color: transparent; -fx-background-radius: 65;");
            button.setStyle("-fx-background-color: rgb(25,160,248,0.3); -fx-background-radius: 65;");

            button.setEffect(null);
        });
        button.setFont(javafx.scene.text.Font.font("KaiTi", javafx.scene.text.FontWeight.BOLD, 40));
        button.setTextFill(javafx.scene.paint.Color.WHITE);
        button.setPrefSize(310, 110);
        button.setStyle("-fx-background-color: rgb(25,160,248,0.3); -fx-background-radius: 65;");
        //button.setStyle("-fx-background-color: transparent; -fx-background-radius: 65;");

        return button;
    }


    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "MAINPANEL");
    }
    @Override
    public void cleanup() {

    }
}
